package org.itboys.mobile.service.mysql.web.cash;

import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mysql.pay.SundayPayConfig;
import org.itboys.mobile.entity.mysql.wallet.SundayWalletRecord;
import org.itboys.mobile.service.mysql.web.pay.SundayPayConfigService;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.itboys.pay.weixin.WeixinPayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * Created by zhangyang on 2018/1/31.
 * 圣代_提现_后台_service
 */
@Service
public class SundayCashService extends BaseMySqlService<SundayWalletRecord>{


    @Autowired
    private SundayPayConfigService payConfigService;

    /**
     * 获取提现申请数据
     * @param request
     * @return
     */
    public PageResult<SundayWalletRecord> select(HttpServletRequest request){
        Map<String,Object> param = ParamUtil.packageMysqlPageParam(request,"accountStatus","accountNo");
        param.put("type", SundayCommonConstants.wallet_record_type_amount_cash);

       // List<SundayWalletRecord> result = super.select(param);
        return super.selectPage(param);
       /* List<SundayWalletRecord> result = super.select(param);
        return new PageResult<SundayWalletRecordDto>(packageDtoMuilt(result),super.count(param));*/
    }

    /**
     * 处理提现申请:同意/驳回
     * @param id    记录id
     * @param accountStatus  状态:2-同意,3-驳回
     */
    public void operate(Long id,Integer accountStatus)throws  Exception{
        if(!LongUtil.isNotZreo(AdminSessionHolder.getAdminUserId())){
            throw new ErrorException("登陆超时，请重新登陆");
        }
        SundayWalletRecord record = super.find(id);
        if(record.getAccountStatus() != SundayCommonConstants.INCOME_YSQ){
            throw new ErrorException("数据状态错误,请联系管理员");
        }
        if(accountStatus!=SundayCommonConstants.INCOME_YTG&&
                accountStatus!=SundayCommonConstants.INCOME_YBH){
            throw new ErrorException("提现状态有误,请联系管理员");
        }
       // record.setAccountStatus(accountStatus);
       // super.update(record);
        //接入微信企业付款。
        //获取支付参数。由于app和微信商城有两个商户账号
        SundayPayConfig payConfig = null;
        if(record.getAccountType()==SundayCommonConstants.pay_type_wechatpay_html||
                record.getAccountType()==SundayCommonConstants.pay_type_wechatpay_open){
            payConfig=payConfigService.getPayConfig(record.getAccountType());
            //付款金额，分
            Integer amount=Integer.valueOf(record.getAmount().multiply(new BigDecimal("-100")).setScale(0,BigDecimal.ROUND_HALF_UP).toString());

           Map<String,String> result =  WeixinPayUtils.companyPay(payConfig.getWeChatAppId(),payConfig.getWeChatMchId(),
                    record.getAccountNo(),
                    record.getAccountOpenId(), "NO_CHECK", null, amount, "提现打款", "118.31.20.82", payConfig.getWeChatApi());
            //返回请求结果
            String return_code=result.get("return_code");
           //返回业务结果
            String result_code = result.get("result_code");

            if(return_code.equalsIgnoreCase("SUCCESS")&&
                    result_code.equalsIgnoreCase("SUCCESS")){
                record.setAccountStatus(accountStatus);
                record.setAccountPayNo(result.get("payment_no"));
                record.setAccountPayTime(result.get("payment_time"));

                super.update(record);
            }else{
                throw new ErrorException(result.get("err_code_des"));
            }
            super.update(record);
        }

    }

   /* *//**
     * 组装dto(多)
     * @param result
     * @return
     *//*
    public List<SundayWalletRecordDto> packageDtoMuilt(List<SundayWalletRecord> result){
        List<SundayWalletRecordDto> dtoList = new ArrayList<SundayWalletRecordDto>();
        for(SundayWalletRecord record : result){
            SundayWalletRecordDto dto = new SundayWalletRecordDto();
            dto.setId(record.getId());
            SundayMember member = memberService.getById(record.getMemberId());
            dto.setCashNickName(member.getNickName());
            dto.setCashUserName(member.getUserName());

            BigDecimal negativeOne = new BigDecimal("-1");
            BigDecimal amount = record.getAmount().multiply(negativeOne);
            dto.setAmount(new DecimalFormat("0.00").format(amount));
            dto.setAccountType(record.getAccountType());
            dto.setAccountStatus(record.getAccountStatus());
            dto.setAccountNo(record.getAccountNo());
            dto.setTime(TimeUtils.format(record.getCt(),"yyyy/MM/dd HH:mm:ss"));
            dtoList.add(dto);
        }
        return dtoList;
    }*/


}
